package com.t235.gc.partners.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.entity.Partners;
import com.t235.gc.partners.service.PartnersService;
import com.t235.gc.partners.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/partners")
public class PartnersController {
    @Autowired
    private PartnersService partnersService;

    @PostMapping("/list")
    public AjaxResult list(Partners par, Integer pageNo, Integer pageSize){
        pageNo = pageNo == null ? 1 :pageNo;
        pageSize = pageSize == null ? 2 : pageSize;
        PageHelper.startPage(pageNo,pageSize);
        List<Partners> list= (List<Partners>) partnersService.list(par);
        PageInfo<Partners> pageInfo=new PageInfo<>(list);
        if(pageInfo!=null){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("10001");
        }
    }

    @PostMapping("/save")
    public AjaxResult save(Partners partners){
        RandomUtils ran=new RandomUtils();
        String num=ran.random(16);
        Long sum=Long.parseLong(num);
        partners.setPartnersId(sum);
        partners.setFid("admin123");
        partners.setIsDel(1);
        partners.setCtime(new Date());
        Integer mum=(int)partnersService.save(partners);
        if(mum>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("10001");
        }
    }

    @PostMapping("/del")
    public AjaxResult del(Integer financeId){
        int num=(int) partnersService.del(financeId);
        if(num>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("10001");
        }
    }

    @PostMapping("/xiu")
    public AjaxResult xiu(Integer partnersId){
        int num = (int) partnersService.xiu(partnersId);
        if(num>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("10001");
        }
    }

    @PostMapping("/update")
    public AjaxResult update(Partners partners){
        int num = (int) partnersService.update(partners);
        if(num>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("10001");
        }
    }

    @PostMapping("/jia")
    public AjaxResult updateJia(Integer balance,Integer financeId){
        int num = (int) partnersService.jia(balance, financeId);
        if(num>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("10001");
        }
    }

    @PostMapping("/jian")
    public AjaxResult updateJian(Integer balance,Integer financeId){
        int num = (int) partnersService.jian(balance, financeId);
        if(num>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("10001");
        }
    }
}
